import {Component, OnInit} from '@angular/core';
import {BusinessStrategyService} from '../business-strategy.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-article-category',
  templateUrl: './article-category.component.html',
  styleUrls: ['./article-category.component.scss']
})
export class ArticleCategoryComponent implements OnInit {
  addCategoryModal = {
    visible: false,
    name: '',
    id: '',
  };
  listOfData = {
    page: 1,
    total: 100,
    records: []
  };

  constructor(
    private bSService: BusinessStrategyService,
    private nzMessageService: NzMessageService
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }

  getTableData() {
    this.bSService.getArticleCategory().then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  saveCategory() {
    if (!this.addCategoryModal.name) {
      this.nzMessageService.error('请输入分类名称');
      return;
    }
    this.bSService.editCategory(this.addCategoryModal.id || null, this.addCategoryModal.name).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideAddCategoryModal();
        this.getTableData();
      }
    });
  }

  editCategory(data: any) {
    this.addCategoryModal = {
      visible: true,
      name: data.classifyName,
      id: data.id,
    };
  }

  addCategory() {
    this.addCategoryModal.visible = true;
  }

  hideAddCategoryModal() {
    this.addCategoryModal = {
      visible: false,
      name: '',
      id: '',
    };
  }

  deleteCategory(id: any) {
    this.bSService.deleteCategory(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      }
    });
  }
}
